package com.example.liunanxin.lnx226ordering.Fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.example.liunanxin.lnx226ordering.R;

import java.util.ArrayList;
import java.util.List;

public class CollectionFragment extends Fragment {
    private ViewPager vp_collection;
    private TabLayout tab_collection;
    private String [] titles = {"店 铺","菜 品"};


    public CollectionFragment() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_collection, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
    }

    private void initView(View v){
        vp_collection = v.findViewById(R.id.viewPager);
        tab_collection = v.findViewById(R.id.tab_layout);

        final List<Fragment> fragments = new ArrayList<>();
        fragments.add(new CollectionFragment_shop());
        fragments.add(new CollectionFragment_food());

        vp_collection.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public Fragment getItem(int i) {
                return fragments.get(i);
            }

            @Override
            public int getCount() {
                return fragments.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return titles[position];
            }
        });

        tab_collection.setupWithViewPager(vp_collection);
    }

}
